﻿using SharpDepend.Datatypes;
using ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer;
using ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine;
using ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine.Modules.Container;
using ZquenceStudio3Host.Graphics.Components;
using ZquenceStudio3Host.Graphics.GUI;
using ZquenceStudio3Host.Resources;

namespace DefaultDspPlugins.UI.Common.Modulizer.Engine.GUIv2.Editors
{
    class BackendToolbar : ScrollPanel
    {
        private double mDpiScale;
        private ModuleManager.ModulizerType mType;
        private bool mPianoShown;
        private Button mLastToolUsed;
        private ModulizerTrackArea mTrackArea;
        private ModulizerUIv2 mModulizerUI;

        private Button pianoButton;
        private Button goOut;
        private Button fillScreen;
        private Button pan;
        private Button select;
        private Button add;

        public BackendToolbar(BasicGuiManager Gui, ModulizerTrackArea trackArea, ModulizerUIv2 modulizerUI)
            : base(Gui, true)
        {
            mTrackArea = trackArea;
            mDpiScale = 1.0;
            mModulizerUI = modulizerUI;
            Color pressedColor = GlobalColors.BaseAudioColor;

            Events.OnScaleChanged += Events_OnScaleChanged;

            Panel previous = null;
            if (mType == ModuleManager.ModulizerType.Synthesizer)
            {
                // Piano.
                pianoButton = new ZquenceStudio3Host.Graphics.Components.Button(Gui);
                pianoButton.BackgroundColor = new Color(65, 65, 65);
                pianoButton.setBgColorPressed(pressedColor);
                pianoButton.setColorIconPressed(Color.Black);
                pianoButton.Width = 100 * mDpiScale;
                pianoButton.setTextureIcon("Images/Icons/piano.png");
                pianoButton.ActiveButton = true;
                pianoButton.IsActivated = true;
                pianoButton.OnActivate += delegate
                {
                    ShowPiano();
                };
                pianoButton.OnDeactivate += delegate
                {
                    HidePiano();
                };
                AddComponent(pianoButton);
                SetPanelAfterH(null, pianoButton);
                ShowPiano();

                previous = pianoButton;
            }

            // Go out.
            goOut = new ZquenceStudio3Host.Graphics.Components.Button(Gui);
            goOut.BackgroundColor = new Color(65, 65, 65);
            goOut.setBgColorPressed(pressedColor);
            goOut.setColorIconPressed(Color.Black);
            goOut.Width = 100 * mDpiScale;
            goOut.setTextureIcon("Images/Icons/zoomout.png");
            goOut.OnClick += GoOut_OnClick;
            AddComponent(goOut);
            SetPanelAfterH(previous, goOut);

            // Fill screen.
            fillScreen = new ZquenceStudio3Host.Graphics.Components.Button(Gui);
            fillScreen.BackgroundColor = new Color(65, 65, 65);
            fillScreen.setBgColorPressed(pressedColor);
            fillScreen.setColorIconPressed(Color.Black);
            fillScreen.Width = 100 * mDpiScale;
            fillScreen.setTextureIcon("Images/Icons/icon_fillscreen.png");
            fillScreen.OnClick += delegate
            {
                Vector4 add = Vector4.Zero;
                add.Width = mTrackArea.GetAbsoluteTrackPositionX(Gui.Display.GraphicsDevice.RenderWidth - mTrackArea.FrontEndPositionX);
                //add.Height = mTrackArea.GetAbsoluteTrackPositionY(Gui.Display.GraphicsDevice.RenderHeight - mTrackArea.FrontEndPositionY);

                mTrackArea.AnimateCameraToFillAllObjects(add);
            };
            AddComponent(fillScreen);
            SetPanelAfterH(goOut, fillScreen);

            pan = new ZquenceStudio3Host.Graphics.Components.Button(Gui);
            select = new ZquenceStudio3Host.Graphics.Components.Button(Gui);
            add = new ZquenceStudio3Host.Graphics.Components.Button(Gui);

            // Pan.
            pan.BackgroundColor = new Color(65, 65, 65);
            pan.setBgColorPressed(pressedColor);
            pan.setColorIconPressed(Color.Black);
            pan.Width = 100 * mDpiScale;
            pan.setTextureIcon("Images/Icons/move2.png");
            pan.ActiveButton = true;
            pan.IsActivated = true;
            pan.OnActivate += delegate
            {
                mLastToolUsed = pan;
                select.IsActivated = false;
                add.IsActivated = false;
                mTrackArea.Tool = ZquenceStudio3Host.TrackArea.ToolButton.None;
            };
            pan.OnDeactivate += delegate
            {
                if (pan == mLastToolUsed)
                {
                    pan.ActivateWithoutEvents();
                }
            };
            AddComponent(pan);
            SetPanelAfterH(fillScreen, pan);
            // Select.
            select.BackgroundColor = new Color(65, 65, 65);
            select.setBgColorPressed(pressedColor);
            select.setColorIconPressed(Color.Black);
            select.Width = 100 * mDpiScale;
            select.setTextureIcon("Images/Icons/icon_select.png");
            select.ActiveButton = true;
            select.IsActivated = false;
            select.OnActivate += delegate
            {
                mLastToolUsed = select;
                pan.IsActivated = false;
                add.IsActivated = false;
                mTrackArea.Tool = ZquenceStudio3Host.TrackArea.ToolButton.Selector;
            };
            select.OnDeactivate += delegate
            {
                if (select == mLastToolUsed)
                {
                    select.ActivateWithoutEvents();
                }
            };
            AddComponent(select);
            SetPanelAfterH(pan, select);
            // Pen.
            add.BackgroundColor = new Color(65, 65, 65);
            add.setBgColorPressed(pressedColor);
            add.setColorIconPressed(Color.Black);
            add.Width = 100 * mDpiScale;
            add.setTextureIcon("Images/Icons/pen.png");
            add.ActiveButton = true;
            add.IsActivated = false;
            add.OnActivate += delegate
            {
                mLastToolUsed = add;
                pan.IsActivated = false;
                select.IsActivated = false;
                mTrackArea.Tool = ZquenceStudio3Host.TrackArea.ToolButton.Pen;
            };
            add.OnDeactivate += delegate
            {
                if (add == mLastToolUsed)
                {
                    add.ActivateWithoutEvents();
                }
            };
            AddComponent(add);
            SetPanelAfterH(select, add);
        }

        private void Events_OnScaleChanged()
        {
            pianoButton.Height = RealHeight;
            goOut.Height = RealHeight;
            fillScreen.Height = RealHeight;
            pan.Height = RealHeight;
            select.Height = RealHeight;
            add.Height = RealHeight;
        }

        /// <summary>
        /// Calls when user want go outside the current module.
        /// </summary>
        private void GoOut_OnClick(object sender, double x, double y, int id)
        {
            var currentModuleContainer = mModulizerUI.CurrentModuleContainer;
            if (currentModuleContainer != null && currentModuleContainer.ParentModule is IModuleContainer)
            {
                mModulizerUI.GoInto(currentModuleContainer.ParentModule);
            
                mTrackArea.Editor.SelectorHandler.ClearSelection();
            
                mTrackArea.AnimateCameraToFillAllObjects();
            }
        }

        private void SetPanelAfterH(Panel afterThis, Panel applyOn)
        {
            if (afterThis == null)
            {
                applyOn.X = 2;
                return;
            }
            applyOn.X = afterThis.X + afterThis.Width + 2 * mDpiScale;
            applyOn.Y = afterThis.Y;
            applyOn.Height = afterThis.Height;
        }

        private void ShowPiano()
        {
            ShowPianoAnimation(true);
        }

        private void HidePiano()
        {
            ShowPianoAnimation(false);
        }

        private void ShowPianoAnimation(bool visibility)
        {
            //mPianoShown = visibility;
            //if (mShowingPianoAnimation == null)
            //{
            //    mShowingPianoAnimation = AnimationUtils<bool>.Run(MENU_ANIMATION_SPEED, PianoShowingTick, AnimationBase.Tanh, visibility);
            //}
            //else
            //{
            //    // Ensure to stop if it is currently animating.
            //    AnimationUtils<bool>.Stop(mShowingPianoAnimation);
            //
            //    AnimationUtils<bool>.ReuseRun(mShowingPianoAnimation, MENU_ANIMATION_SPEED, PianoShowingTick, visibility);
            //}
        }
    }
}
